/**
    ATClab - scriptable Air Traffic Control simulations.
    Copyright (C) 2010 openatclab@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

#ifndef __ATC_CLOCK_HDR__
#define __ATC_CLOCK_HDR__


#include <qobject.h>
#include <qdatetime.h>

#include "atc_types.h"

#include <vector>

class QTimer;

namespace atc {

    //
    // atc::Clock
    //

    class Clock : public QObject {
		Q_OBJECT

	private:

		struct Alarm {

			Alarm( QTimer *t, int i, int e ) 
					: timer( t ), timeout( i ), elapsed( e ) 
			{};

			QTimer *timer;
			int     timeout;
			int     elapsed;
		};

		typedef std::vector< Alarm >::iterator AlarmIt;

    public:
        Clock();
        ~Clock();

    public:
		void set_interval( int ms ) { _tickAt   = ms; }
		void set_multiple( float x )  { _xspeed = x; }

		void set_alarm( int, QObject *, const char * );
		long elapsed_time();

	public slots:
		void start();
		void resume();
		void pause();
		void stop();

	private slots:
		void tick_dispatch();

	signals:
		void started();
		void paused();
		void resumed();
		void stopped();
		void tick( int );
		void seconds( long );
		void event_recorded( KeyValues & );

	private:
		QTime   _time;
		QTimer *_timer;
		std::vector< Alarm > _alarms;
		bool    _running;
		int     _tickAt;
		long    _elapsed;
		long	_milliseconds;
		float   _xspeed;
	};


};

#endif
